#include<stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
    //保存管道的两个文件描述符
    int fd[2] = {0};
    int r = pipe(fd);
    if(r ==-1)
    {
        perror("create pipe failed");
    }
    pid_t pid = fork();

    if(pid == -1)
    {
        perror("pid failed");
    }
    else if(pid >0)
    {
        //父进程
        printf("father pid=%d,son pid =%d\n",getpid(),pid);
        write(fd[1],"alabasitadiguoguhuaid!",25);

        
    }
    else if(pid == 0)
    {
        //子进程
        printf("son pid=%d,father pid =%d\n",getpid(),getppid());
        char buf[64] = {0};
        r = read(fd[0],buf,15); 
        printf("r = %d\n",r);
        printf("buf =%s\n",buf);

        r = read(fd[0], buf, 10);
        printf("r = %d\n", r);
        printf("buf =%s\n", buf);

    }
    wait(NULL);
        close(fd[0]);
        close(fd[1]);
    return 0;
}